/*
 * *****************************************************************************
 *  Copyright ( c ) 2019 huibur Inc. All Rights Reserved.
 *
 *  This software is the confidential and proprietary information of Huibur Inc
 *  (*Confidential Information*). You shall not disclose such Confidential Information
 *  and shall use it only in accordance with the terms of the license agreement
 *  you entered into with Huibur or a Huibur authorized
 *  reseller (the "License Agreement*).
 *  ******************************************************************************
 */


package com.huibur.devicecheck.controller;

import com.huibur.devicecheck.enums.ResultCodeEnum;
import com.huibur.devicecheck.exception.TaskRecordAlreadyExistException;
import com.huibur.devicecheck.model.param.TaskRecordParam;
import com.huibur.devicecheck.model.query.TaskRecordQuery;
import com.huibur.devicecheck.model.vo.RecordStatusVO;
import com.huibur.devicecheck.model.vo.TaskRecordVO;
import com.huibur.devicecheck.request.ListRes;
import com.huibur.devicecheck.request.ObjRes;
import com.huibur.devicecheck.request.PageRes;
import com.huibur.devicecheck.service.ITaskRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

/*******************************************************
* Title: TaskRecordController.java
* Description: 
* @author baiqingshuo
* @date 2021/9/23
* @version 1.0
*******************************************************/
@RestController
@RequestMapping("/taskRecords")
@Api(value = "检修记录",tags = {"检修记录"})
@Slf4j
public class TaskRecordController {

    @Autowired
    ITaskRecordService iTaskRecordService;

    /**
     * 新增检修记录
     *
     * @param taskRecordParam
     * @return
     */
    @PostMapping("")
    @ApiOperation(value = "新增检修记录", response = ObjRes.class)
    public ObjRes<Void> insert(@RequestBody TaskRecordParam taskRecordParam) {
      try {
          return iTaskRecordService.insert(taskRecordParam);
      } catch (TaskRecordAlreadyExistException e) {
          log.error(e.getMessage());
          return new ObjRes(ResultCodeEnum.TASK_RECORD_ALREADY_EXIST.getCode(), ResultCodeEnum.TASK_RECORD_ALREADY_EXIST.getLabel());
      }

    }

    /**
     * 删除检修记录
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除检修记录", response = ObjRes.class)
    public ObjRes<Boolean> deleteById(@PathVariable Long id) {
        return new ObjRes<Boolean>().data(iTaskRecordService.deleteById(id));
    }

    /**
     * 编辑检修记录
     *
     * @param id
     * @param taskRecordParam
     * @return
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "编辑检修记录", response = ObjRes.class)
    public ObjRes<Void> updateTaskRecord(@PathVariable Long id, @RequestBody TaskRecordParam taskRecordParam) {
       try {
           return iTaskRecordService.updateTaskRecord(id, taskRecordParam);
       } catch (TaskRecordAlreadyExistException e) {
           log.error(e.getMessage());
           return new ObjRes(ResultCodeEnum.TASK_RECORD_ALREADY_EXIST.getCode(), ResultCodeEnum.TASK_RECORD_ALREADY_EXIST.getLabel());
       }

    }

    /**
     * 检修记录列表(查询并分页)
     * @param taskRecordQuery
     * @return
     */
    @GetMapping("")
    @ApiOperation(value = "检修记录列表(查询并分页)", response = PageRes.class)
    public PageRes<TaskRecordVO> selectListTaskRecord(TaskRecordQuery taskRecordQuery) {
        return iTaskRecordService.selectListTaskRecord(taskRecordQuery);
    }

    /**
     * 获取检修记录详情
     *
     * @param id
     * @return
     */
    @GetMapping("/selectTaskRecordById/{id}")
    @ApiOperation(value = "获取检修记录详情", response = ObjRes.class)
    public ObjRes<TaskRecordVO> selectTaskRecordById(@PathVariable Long id) {
        return new ObjRes<TaskRecordVO>().data(iTaskRecordService.selectByListTaskRecord(id));
    }

    /**
     * 根据用户id查询检修记录
     *
     * @param id
     * @return
     */
    @GetMapping("/findTaskRecordByUserId/{id}")
    @ApiOperation(value = "根据用户id查询检修记录",response = ListRes.class)
    public ListRes<TaskRecordVO> findTaskRecordByUserId(@PathVariable Long id) {
        return new ListRes<TaskRecordVO>().data(iTaskRecordService.findTaskRecordByUserId(id));
    }

    /**
     * 根据设备label查询检修记录
     *
     * @param deviceLabel
     * @return
     */
    @GetMapping("/findTaskRecordByDeviceLabel")
    @ApiOperation(value = "根据设备唯一标识查询检修记录",response = ListRes.class)
    public ListRes<TaskRecordVO> findTaskRecordByDeviceLabel(@RequestParam String deviceLabel) {
        return new ListRes<TaskRecordVO>().data(iTaskRecordService.findTaskRecordByDeviceLabel(deviceLabel));
    }

    /**
     * 根据任务id查询检修记录
     * @param id
     * @return
     */
    @GetMapping("/findTaskRecordByTaskId/{id}")
    @ApiOperation(value = "根据任务id查询检修记录",response = ListRes.class)
    public ListRes<TaskRecordVO> findTaskRecordByTaskId(@PathVariable Long id) {
        return new ListRes<TaskRecordVO>().data(iTaskRecordService.findTaskRecordByTaskId(id));
    }

    /**
     * 后台管理端首页统计图（状态统计）
     * @return
     */
    @GetMapping("/statistics")
    @ApiOperation(value = "后台管理端首页统计图（状态统计）",response = ObjRes.class)
    public ObjRes<RecordStatusVO> statistics() throws ParseException {
        return new ObjRes<RecordStatusVO>().data(iTaskRecordService.statistics());
    }
}